package com.web.dao;

import com.github.pagehelper.Page;
import com.web.controller.web.MskBuyOrderController;
import com.web.entity.MskBuyOrder;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author FDjavaone
 */
public interface MskBuyOrderMapper {


    @Delete({
            "delete from t_msk_buy_order",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into t_msk_buy_order (m_id, name, ",
            " phone, core_points, ",
            " deal_time, deal_price, ",
            " deal_num, status, ",
            " is_first, create_time, ",
            " modify_time)",
            " values (#{mId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
            " #{phone,jdbcType=VARCHAR}, #{corePoints,jdbcType=DECIMAL}, ",
            " #{dealTime,jdbcType=TIMESTAMP}, #{dealPrice,jdbcType=DECIMAL}, ",
            " #{dealNum,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, ",
            " #{isFirst,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, ",
            " #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(MskBuyOrder record);

    @InsertProvider(type = MskBuyOrderSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(MskBuyOrder record);


    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    MskBuyOrder selectByPrimaryKey(Long id);


    @UpdateProvider(type = MskBuyOrderSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(MskBuyOrder record);

    @Update({
            "update t_msk_buy_order",
            " set m_id = #{mId,jdbcType=BIGINT},",
            " name = #{name,jdbcType=VARCHAR},",
            " phone = #{phone,jdbcType=VARCHAR},",
            " core_points = #{corePoints,jdbcType=DECIMAL},",
            " deal_time = #{dealTime,jdbcType=TIMESTAMP},",
            " deal_price = #{dealPrice,jdbcType=DECIMAL},",
            " deal_num = #{dealNum,jdbcType=INTEGER},",
            " status = #{status,jdbcType=INTEGER},",
            " is_first = #{isFirst,jdbcType=INTEGER},",
            " create_time = #{createTime,jdbcType=TIMESTAMP},",
            " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(MskBuyOrder record);

    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where m_id = #{id,jdbcType=BIGINT} and status=0"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<MskBuyOrder> selectByMid(Long id);

    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where  status=0 order by create_time asc limit 13"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<MskBuyOrder> selectAhead13();


    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where m_id = #{mId,jdbcType=BIGINT} and status=0"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<MskBuyOrder> selectUnfinishOrder(MskBuyOrder order);

    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where m_id = #{mId,jdbcType=BIGINT} and status=2"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<MskBuyOrder> selectFinishOrder(MskBuyOrder order);


    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where  status=0 order by id asc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<MskBuyOrder> selectUnDealOrder();

    @Select({
            "select",
            " id, m_id, name, phone, core_points, deal_time, deal_price, deal_num, status, ",
            " is_first, create_time, modify_time",
            " from t_msk_buy_order",
            " where  status=0 and is_first=1"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    MskBuyOrder selectUnDealAndFirst();

    @SelectProvider(type = MskBuyOrderSqlProvider.class, method = "selectAllMskBuyOrder")
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "m_id", property = "mId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "core_points", property = "corePoints", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_time", property = "dealTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "deal_num", property = "dealNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_first", property = "isFirst", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Page<MskBuyOrder> selectAllMskBuyOrder(MskBuyOrderController.MskBuyColumns mskBuyColumns);

    @Update({
            "update t_msk_buy_order",
            " is_first = 0,",
            " modify_time = now()",
            " where status = 0"
    })
    void resetIsFirstMskBuyOrder();
}